package com.activity.activitybpm.controller.bpm.process;

import cn.dev33.satoken.util.SaResult;
import lombok.extern.log4j.Log4j2;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/process/instance")
@Log4j2
public class ProcessInstanceController {

    @PostMapping(value = "/run")
    public SaResult run() {
        // 1. 实例化流程引擎实例
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取 RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 开启流程实例 (流程设计图唯一标识key）
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("level");
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        return SaResult.ok().setData(processInstance.getProcessDefinitionId());
    }
}
